<?php
class category extends ujn_controller {

	function index() { // 显示栏目列表
		$siteid = $this->get('siteid', 1);
		$pid = $this->get('pid', 0);
		$cid = $this->get('cid', 0);
		$catid = $this->get('catid', 0);
		$page = $this->get('page', 1);
		$do = $this->get('do', '');
		
		$this->assign('is_end', !empty($catid));
		
		$page_max = 20;
		
		$privs = !empty($do) ? array() : mdl_category::get_instance()->get_priv();
		$datas = mdl_category::get_instance()->get_sub($siteid, $privs);
		$top_cats = array();
		$sub_cats = array();
		$cil_cats = array();
		foreach($datas as $k => $d) { // 最顶级
			if(empty($d['parentid'])) {
				$top_cats[] = $d;
			}
			
			if(!empty($pid) && $d['parentid'] == $pid) {
				$sub_cats[] = $d;
			}
			
			if(!empty($cid) && $d['parentid'] == $cid && $cid != $pid) {
				$cil_cats[] = $d;
			}
		}
		
		$this->assign('pid', $pid);
		$this->assign('cid', $cid);
		$this->assign('catid', $catid);
		
		$this->assign('cats', $top_cats);
		$this->assign('sub_cats', $sub_cats);
		$this->assign('cil_cats', $cil_cats);
		
		$caid = 0;
		if(!empty($catid)) $caid = $catid;
		elseif(!empty($cid)) $caid = $cid;
		elseif(!empty($pid)) $caid = $pid;
		$this->assign('caid', $caid);
		
		if(isset($datas[$caid])) {
			$curr_info = $datas[$caid];
			$parentid = $datas[$caid]['parentid'];
			$caid = explode(',', $datas[$caid]['arrchildid']);

			if(!empty($privs)) 
				foreach($caid as $k => $v) { // 权限过滤
					if(!isset($privs[$v])) unset($caid[$k]);
				}
				
			$arrparentid = explode(',', $curr_info['arrparentid']);
			$curr_info['p_path'] = array();
			foreach($arrparentid as $v) {
				if(!empty($v))
					$curr_info['p_path'][] = $datas[$v];
			}
		} else {
			$curr_info = array();
			$caid = array();
		}

		$data = mdl_news::get_instance()->get_catids($caid, ($page-1) * $page_max, $page_max);
		$count = $data['count'];
		$data = $data['data'];
		$ids = array();
		foreach($data as $k => $d) {
			$ids[] = $d['id'];
// 			$fdata = mdl_news::get_instance()->get_file($d['id']);
// 			$data[$k]['fileurl'] = $fdata['data']['fileurl'];
// 			$data[$k]['author'] = $fdata['data']['author'];
// 			$tags = mdl_news::get_instance()->get_tags($d['id'],$siteid);
// 			$data[$k]['tags'] = $tags;		
			$data[$k]['cat_info'] = $datas[$d['catid']];
			$data[$k]['cat_info']['arrparentid'] = explode(',', $datas[$d['catid']]['arrparentid']);
		}
		
		$fdatas = array();
		foreach(mdl_news::get_instance()->get_files($ids) as $d) {
			$fdatas[$d['id']] = $d;
		}
		
		$tdatas = array();
		foreach(mdl_news::get_instance()->get_tagss($ids) as $d) {
			if(!isset($tdatas[$d['contentid']])) $tdatas[$d['contentid']] = array();
			$tdatas[$d['contentid']][] = $d;
		}
		
		$hits = array();
		foreach(mdl_news::get_instance()->get_hit($ids) as $d) {
			list($null, $null, $id) = explode('-', $d['hitsid']);
			$hits[$id] = $d;
		}
		
		foreach($data as $k => $d) {
			$data[$k]['hits'] = isset($hits[$d['id']]) ? $hits[$d['id']]['views'] : 0;
			$data[$k]['fileurl'] = isset($fdatas[$d['id']]) ? $fdatas[$d['id']]['fileurl'] : '';
			$data[$k]['author'] = isset($fdatas[$d['id']]) ? $fdatas[$d['id']]['author'] : '';
			$data[$k]['tags'] = isset($tdatas[$d['id']]) ? $tdatas[$d['id']] : array();
		}
		$this->assign('artlist', $data);
		
		// 简单分页
		$pages = array();
		$max_page = 0;
		if($count > 0) {
			$max_page = ceil($count / $page_max);
			$this->assign('page', $page);
			$this->assign('count', $count);
			$this->assign('max_page', $max_page);
			
			$pages[] = 1; // first
			if($page > $max_page - 4) $start_page = $page - 3;
			else $start_page = $page - 3;
			
			if($start_page < 2) $start_page = 2;
			if($start_page >= $max_page - 6) $start_page = $max_page - 7;
			
			for($i=$start_page; $i<$start_page + 7; $i++) {
				if($i > 1 && $i < $max_page)
					$pages[] = $i;
			}
			$pages[] = $max_page; // last
		} else {
			$this->assign('page', 0);
			$this->assign('count', 0);
			$this->assign('max_page', 0);
		}
		
		$this->assign('pages', $pages);
		
		$sd = mdl_category::get_instance()->mysql()->find_one("select * from `v9_site` where siteid='{$siteid}' limit 1;");
		
		if(empty($curr_info['description'])) {
			$curr_info['description'] = $sd['description'];
		}
		$curr_info['page'] = $page;
		$curr_info['keywords'] = $sd['keywords'];
		$curr_info['title'] = isset($curr_info['catname']) ? $curr_info['catname'] : '';

		$this->assign('curr_info', $curr_info);
		if($pid){
			$this->set_view_path('category/index_list.html');
		}
		
		switch($do) {
			case 'preview':
				if($parentid){
					$this->set_view_path('category/list.html');

					//当前频道右侧列表数据
					$rs_arr = array(
						'10012' => array('1490', '1491'),
					);
					$rs_list = array();
					if($rs_arr[$pid]){
						foreach($rs_arr[$pid] as $k => $v){
							$r_x = $datas[$v]; //mdl_category::get_instance()->get($v);
							$r_cat = explode(',', $r_x['arrchildid']);
							$s_x = mdl_category::get_instance()->find_downloads($r_cat, $siteid,2);
							$rs_list[$v] = $s_x;
						}
					}

				}else{
					$this->set_view_path('category/category.html');
					
					//当前频道子栏目列表数据
					$s_arr = array(
						'10001' => array('830'),
						'10012' => array('79', '96', '124', '132', '408', '882', '1061', '1416', '1579', '2340', '13381'),
						'11003' => array('282', '345', '373', '1398', '1402', '1409'),
						'11009' => array('1255', '1256', '1257', '1258', '1259', '1444'),
						'11015' => array('832', '833', ' 834', '836', '837', '869', '884'),
						'11019' => array('917', '918', '923', '924', '929'),
						'11026' => array('1553', '1554', '1555', '1556', '1557', '1567', '1588'),
						'11028' => array('1265', '1266', '1267', '1705'),
						'11031' => array('1610', '1611', '1689', '1690', '1691', '1698'),
						'11032' => array('1622', '1623', '1624','1625', '1626', '1657'),
						'11033' => array('1643', '1644', '1645', '1706'),
						'11040' => array('2641', '2659', '2660', '13194'),
						'13075' => array('13077', '13084', '13086', '13087', '13088', '13089')
					);
					$sub_list = array(); 
					if($s_arr[$pid]){
						foreach($s_arr[$pid] as $key => $v){
							$cx = $datas[$v]; //mdl_category::get_instance()->get($v);
							$s_cat = explode(',', $cx['arrchildid']);
							$sx = mdl_category::get_instance()->find_sub($s_cat, $siteid,4);
							foreach($sx as $k => $d){
								$sx[$k]['k'] = $k+1;
							}
							
							$sub_list[$v] = $sx;
						}
					}
					$this->assign('sub_list', $sub_list);
					
					// 频道的文章导读
					$c_arr = array(
						'10001' => array('239', '1843', '1845', '1880', '2123'),
						'10011' => array('402', '1804', '1806', '2058'),
						'11001' => array('817', '1461', '1487'),
						'11012' => array('788', '789', '900', '1455')
					);
					$dd_list = array(); 
					if($c_arr[$pid]){
						foreach($c_arr[$pid] as $key => $v){
							$ddx = $datas[$v]; //mdl_category::get_instance()->get($v);
							$dd_cat = explode(',', $ddx['arrchildid']);
							$dsx = mdl_category::get_instance()->find_sub($dd_cat, $siteid,7);
							foreach($dsx as $k => $d){
								$dsx[$k]['k'] = $k+1;
							}
							
							$dd_list[$v] = $dsx;
						}
					}
					$this->assign('dd_list', $dd_list);
					
					// 频道的图文导读
					$img_list = array(); 
					if($c_arr[$pid]){
						foreach($c_arr[$pid] as $key => $v){
							$idx = $datas[$v]; //mdl_category::get_instance()->get($v);
							$id_cat = explode(',', $idx['arrchildid']);
							$idsx = mdl_category::get_instance()->find_sub_img($id_cat, $siteid,1);
							$img_list[$v] = $idsx;
						}
					}
					$this->assign('img_list', $img_list);

					$cats = mdl_category::get_instance()->get($pid);
					$catids = $cats['arrchildid'];
					// 当前频道的总排行数据
					$x = mdl_category::get_instance()->find_list_all($catids, $siteid,8);
					foreach($x as $k => $d){
						$x[$k]['k'] = $k+1;
					}
					$this->assign('all_list', $x); 
					// 当前频道的本月的排行数据
					$x = mdl_category::get_instance()->find_list_mon($catids, $siteid,8);
					foreach($x as $k => $d){
						$x[$k]['k'] = $k+1;
					}
					$this->assign('mon_list', $x); 
				
					// 最新下载列表
					$d_arr = array(
						'10011' => array('1764'),
						'10012' => array('1490', '1491'),
						'11019' => array('537')
					);
					$dow_list = array(); 
					if(isset($d_arr[$pid])){
						foreach($d_arr[$pid] as $key => $v){
							$dx = $datas[$v]; //mdl_category::get_instance()->get($v);
							$d_cat = explode(',', $dx['arrchildid']);
							$dowx = mdl_category::get_instance()->find_downloads($d_cat, $siteid,4);
							foreach($dowx as $k => $d){
								$dowx[$k]['k'] = $k+1;
							}		
							$dow_list[$v] = $dowx;
						}
					}

				}
				$out_file = "{$curr_info['parentdir']}{$curr_info['catdir']}/";
				$this->assign('caturl', $out_file);
		
				break;
			case 'makestatic':
				if($parentid){
					$this->set_view_path('category/list.html');

					//当前频道右侧列表数据
					$rs_arr = array(
						'10012' => array('1490', '1491'),
					);
					$rs_list = array();
					if(isset($rs_arr[$pid])){
						foreach($rs_arr[$pid] as $k => $v){
							$r_x = $datas[$v]; //mdl_category::get_instance()->get($v);
							$r_cat = explode(',', $r_x['arrchildid']);
							$s_x = mdl_category::get_instance()->find_downloads($r_cat, $siteid,2);
							$rs_list[$v] = $s_x;
						}
					}

					$this->assign('rs_list', $rs_list);

				}else{
					$this->set_view_path('category/category.html');

					//当前频道子栏目列表数据
					$s_arr = array(
						'10001' => array('830'),
						'10012' => array('79', '96', '124', '132', '408', '882', '1061', '1416', '1579', '2340', '13381'),
						'11003' => array('282', '345', '373', '1398', '1402', '1409'),
						'11009' => array('1255', '1256', '1257', '1258', '1259', '1444'),
						'11015' => array('832', '833', ' 834', '836', '837', '869', '884'),
						'11019' => array('917', '918', '923', '924', '929'),
						'11026' => array('1553', '1554', '1555', '1556', '1557', '1567', '1588'),
						'11028' => array('1265', '1266', '1267', '1705'),
						'11031' => array('1610', '1611', '1689', '1690', '1691', '1698'),
						'11032' => array('1622', '1623', '1624','1625', '1626', '1657'),
						'11033' => array('1643', '1644', '1645', '1706'),
						'11040' => array('2641', '2659', '2660', '13194'),
						'13075' => array('13077', '13084', '13086', '13087', '13088', '13089')
					);
					$sub_list = array(); 
					if(isset($s_arr[$pid])){
						foreach($s_arr[$pid] as $key => $v){
							$cx = $datas[$v]; //mdl_category::get_instance()->get($v);
							$s_cat = explode(',', $cx['arrchildid']);
							$sx = mdl_category::get_instance()->find_sub($s_cat, $siteid,4);
							foreach($sx as $k => $d){
								$sx[$k]['k'] = $k+1;
							}
							
							$sub_list[$v] = $sx;
						}
					}
					$this->assign('sub_list', $sub_list);
					
					// 频道的文章导读
					$c_arr = array(
						'10001' => array('239', '1843', '1845', '1880', '2123'),
						'10011' => array('402', '1804', '1806', '2058'),
						'11001' => array('817', '1461', '1487'),
						'11012' => array('788', '789', '900', '1455')
					);
					$dd_list = array(); 
					if(isset($c_arr[$pid])){
						foreach($c_arr[$pid] as $key => $v){
							$ddx = $datas[$v]; //mdl_category::get_instance()->get($v);
							$dd_cat = explode(',', $ddx['arrchildid']);
							$dsx = mdl_category::get_instance()->find_sub($dd_cat, $siteid,7);
							foreach($dsx as $k => $d){
								$dsx[$k]['k'] = $k+1;
							}
							
							$dd_list[$v] = $dsx;
						}
					}
					$this->assign('dd_list', $dd_list);
					
					// 频道的图文导读
					$img_list = array(); 
					if(isset($c_arr[$pid])){
						foreach($c_arr[$pid] as $key => $v){
							$idx = $datas[$v]; //mdl_category::get_instance()->get($v);
							$id_cat = explode(',', $idx['arrchildid']);
							$idsx = mdl_category::get_instance()->find_sub_img($id_cat, $siteid,1);
							$img_list[$v] = $idsx;
						}
					}
					$this->assign('img_list', $img_list);

					$cats = isset($datas[$pid]) ? $datas[$pid] : array(); // mdl_category::get_instance()->get($pid);
					$catids = isset($cats['arrchildid']) ? $cats['arrchildid'] : '';
					// 当前频道的总排行数据
					$x = mdl_category::get_instance()->find_list_all($catids, $siteid,8);
					foreach($x as $k => $d){
						$x[$k]['k'] = $k+1;
					}
					$this->assign('all_list', $x); 
					// 当前频道的本月的排行数据
					$x = mdl_category::get_instance()->find_list_mon($catids, $siteid,8);
					foreach($x as $k => $d){
						$x[$k]['k'] = $k+1;
					}
					$this->assign('mon_list', $x); 
				
					// 最新下载列表
					$d_arr = array(
						'10011' => array('1764'),
						'10012' => array('1490', '1491'),
						'11019' => array('537')
					);
					$dow_list = array(); 
					if(isset($d_arr[$pid])){
						foreach($d_arr[$pid] as $key => $v){
							$dx = $datas[$v]; //mdl_category::get_instance()->get($v);
							$d_cat = explode(',', $dx['arrchildid']);
							$dowx = mdl_category::get_instance()->find_downloads($d_cat, $siteid,4);
							foreach($dowx as $k => $d){
								$dowx[$k]['k'] = $k+1;
							}		
							$dow_list[$v] = $dowx;
						}
					}
					$this->assign('dow_list', $dow_list); 
				}
				
				$out_file = mdl_category::get_instance()->make_static_path($curr_info);
				$this->assign('caturl', $out_file);

// 				// make!
// 				$out_file = "{$curr_info['parentdir']}{$curr_info['catdir']}/";
// 				if($page == '1') {
// 					$out_file .= 'index.shtml';
// 				} elseif($max_page > 0) {
// 					$out_file .= 'List_' . ($max_page - $page + 1) . '.shtml';
// 				}
				ujn::bind_cfg(array('output_static_html' => $out_file));
				
				if($max_page > 1) {
					ujn::bind_cfg(array('output_params' => array('max_page' => $max_page)));
				}
				break;
			case 'previewx':
			case 'makestaticx':
				die('??');
		}
	}
	
	function get_index() {
		$siteid = $this->get('siteid', 1);
		$caid = $this->get('catid', 0);
		$datas = mdl_category::get_instance()->get_sub($siteid, array());
		$x = $datas[$caid];
		
		$xids = explode(',', $datas[$caid]['arrchildid']);
		
		$data = mdl_news::get_instance()->get_catids($xids, 0, 100000, 1, 'id');
		
		$ids = array();
		foreach($data['data'] as $d) {
			$ids[] = $d['id'];
		}
		die(json_encode(array('catid' => $caid, 'ids' => $ids)));
	}
	
	function make_home() {
		// 生成首页
		$siteid = $this->get('siteid', 1);
		
		$this->set_view_path('tpl/home.html');
		ujn::bind_cfg(array('output_static_html' => '/index.shtml'));
		
		//站带seo信息
		$seo = mdl_category::get_instance()->mysql()->find_one("select * from `v9_site` where siteid='{$siteid}' limit 1;");
		$this->assign('seo', $seo);

		//首页推荐列表
		$t_arr = array('2753', '13403', '10001', '10011', '124', '96', '11001', '345', '11012', '11032', '13418', '11039');
		$t_list = array(); 
		foreach($t_arr as $key => $v){
			$x = mdl_category::get_instance()->get($v);
			$cat = explode(',', $x['arrchildid']);
			$tx = mdl_category::get_instance()->find_sub($cat, $siteid,1);
			$t_list[$v] = $tx;
		}
		$this->assign('t_list', $t_list);
		
		//首页各频道列表
		$p_arr = array('10001', '10011', '10012', '11001', '11003', '11010', '11012', '11015', '11032', '13264');
		$p_list = array(); 
		foreach($p_arr as $key => $v){
			$px = mdl_category::get_instance()->get($v);
			$pcat = explode(',', $px['arrchildid']);
			$dx = mdl_category::get_instance()->find_sub($pcat, $siteid,6);
			foreach($dx as $k => $d) {
				//$dx[$k]['description'] = htmlspecialchars_decode($this->_str_cut($d['description'],150));
				$fdata = mdl_news::get_instance()->get_file($d['id']);
				$dx[$k]['fileurl'] = $fdata['data']['fileurl'];
				$dx[$k]['author'] = $fdata['data']['author'];
				$tags = mdl_news::get_instance()->get_tags($d['id'],$siteid);
				$dx[$k]['tags'] = $tags;	
			}
			$p_list[$v] = $dx;
		}
		ujn::bind_cfg(array('output_params' => array('siteid' => $siteid)));
		$this->assign('p_list', $p_list);
	}
	
	function make_all() {
		
		$siteid = 1;
		
		$datas = mdl_category::get_instance()->get_sub($siteid, array());
		$sub_cats = array();
		foreach($datas as $k => $d) { // 最顶级
			if($d['parentid'] == 0) {
				$sub_cats[] = $d;
			}
		}
		
		foreach($sub_cats as $key => $d) {
			$caid = explode(',', $d['arrchildid']);
			$sub_cats[$key]['child'] = array();
			foreach($caid as $k => $v) {
				$datas[$v]['child'] = array();
				$_caid = explode(',', $datas[$v]['arrchildid']);
				foreach($_caid as $kk => $dd) {
					if($dd != $v && isset($datas[$dd])) {
						$datas[$v]['child'][] = $datas[$dd];
					}
				}
				if($sub_cats[$key]['catid'] == $datas[$v]['parentid'])
					$sub_cats[$key]['child'][] = $datas[$v];
			}
		}
		
		$this->assign('sub_cats', $sub_cats);
	}
}